/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mavenrentcars.service.impl;

import com.mavenrentcars.dao.RoleDao;
import com.mavenrentcars.model.Role;
import com.mavenrentcars.model.User;
import com.mavenrentcars.service.RoleService;
import java.util.HashMap;
import java.util.List;
import javax.annotation.Resource;
import org.json.simple.JSONObject;
import org.springframework.stereotype.Service;

/**
 *
 * @author admin
 */
@Service("roleService")
public class RoleServiceImpl implements RoleService {

    @Resource(name = "roleDao")
    RoleDao roleDao;

    @Override
    public Role getByAccountAndRole(User account, String string) {
        return roleDao.getByAccountAndRole(account, string);
    }

    @Override
    public List<Role> getByAccount(User account) {
        return roleDao.getByAccount(account);
    }

    @Override
    public Role save(Role model) {
        return roleDao.save(model);
    }

    @Override
    public Role get(Integer id) {
        return roleDao.get(id);
    }

    @Override
    public void remove(Integer id) {
        roleDao.remove(id);
    }

    @Override
    public List<Role> getAll() {
        return roleDao.getAll();
    }

    @Override
    public String getJsonRoles() {

        JSONObject jsonObject = new JSONObject();
        List<Role> roles = getAll();
        for (Role role : roles) {
            HashMap<String, String> rmap = new HashMap<String, String>();
            rmap.put("id", Integer.toString(role.getId()));
            rmap.put("name", role.getRoleName());
            rmap.put("action", "add");
            jsonObject.put(rmap.get("id"), rmap);
        }

        return jsonObject.toString();
    }
}
